
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class code {

  
			public static BufferedWriter os = null;
			public static BufferedReader is = null;
			
			public static Socket clientSocket = null;	
  
			public static void main (String [] args ) throws IOException 
			{
		  
					final String serverHost = "localhost";
	    
					Socket clientSocket = null;
		 
					try {   
	        
							clientSocket = new Socket(serverHost, 8081);
		 
							// Create output stream at the client (to send data to the server)
							os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

							// Input stream at Client (Receive data from the server).
							is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	    	 	
							send("SEND");
							
						   
							
/*							
							  byte[] contents = new byte[10000];
					            
					            //Initialize the FileOutputStream to the output file's full path.
					            FileOutputStream fos = new FileOutputStream("c:/temp/plouf.jpg");
					            BufferedOutputStream bos = new BufferedOutputStream(fos);
					            InputStream ins = clientSocket.getInputStream();
					            
					            //No of bytes read in one read() call
					            int bytesRead = 0; 
					            
					            while((bytesRead=ins.read(contents))!=-1)
					                bos.write(contents, 0, bytesRead); 
					            
					            bos.flush(); 
					           
					            System.out.println("File saved successfully!"); 
*/							
							// Thread stuffer = new StuffThread(clientSocket);
						    //  stuffer.start();
							
							String userInput;
							
							/*
							
							 String line = in.readLine();
					            while( line != null )
					            {
					                System.out.println( line );
					                line = in.readLine();
					            }
*/
							
						    // BufferedReader stdIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

						  
						     /*   while ((userInput = is.readLine()) != null)
						        {
						           
						            System.out.println("Response from server:");
						        	
						        
						        }*/
							
							// receive file
/*							int bytesRead;
						    int current = 0;
						    FileOutputStream fos = null;
						    BufferedOutputStream bos = null;
						   String
						       FILE_TO_RECEIVED = "c:/temp/coco.jpg";  // you may change this, I give a
						                                                            // different name because i don't want to
						                                                            // overwrite the one used by server...

						 int FILE_SIZE = 6022386; // file size temporary hard coded
						                                               // should bigger than the file to be downloaded
							
							
						    byte [] mybytearray  = new byte [FILE_SIZE];
						    InputStream is = clientSocket.getInputStream();
						    fos = new FileOutputStream(FILE_TO_RECEIVED);
						    bos = new BufferedOutputStream(fos);
						    bytesRead = is.read(mybytearray,0,mybytearray.length);
						    current = bytesRead;

						    do {
						         bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
						         if(bytesRead >= 0) current += bytesRead;
						      } while(bytesRead > -1);

						      bos.write(mybytearray, 0 , current);
						      bos.flush();
						      System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
				*/			
							
					}
	     
	     
				     catch (UnknownHostException e)
				     {
				           System.err.println("Don't know about host " + serverHost);
				            return;
				     } 
				     catch (IOException e)
				     {
				            System.err.println("Couldn't get I/O for the connection to " + serverHost);
				            return;
				     }
	     
	     
	     try {
	    	 	os.close();
	    	 	is.close();
	    	 	clientSocket.close();
	     }
	     
	     catch (UnknownHostException e)
	     {
	            System.err.println("Trying to connect to unknown host: " + e);
	     } 
	     catch (IOException e) 
	     {
	            System.err.println("IOException:  " + e);
	     } 
	     
  }
  
  
		  //simple fonction 
		  private static void send(String message)
		  {
		  	
		  	 try {
		           if (os != null) {
		
		 	    	  os.write(message);
		 	          os.flush();
		 	         System.out.println ("Message envoy au serveur");
		           }
		        }
		        catch (IOException e) { os = null; } 
		  	
		  
		
		  }




		  private static void receive() throws IOException
		{
			
			 // receive file
		
			int filesize=6022386; 
			int bytesRead;
		    int current = 0;
		    byte [] mybytearray  = new byte [filesize];
		  //  InputStream is = clientSocket.getInputStream();
		    
		    File myFile;
		    
		    myFile=new File("img.jpg");
		    ObjectOutputStream oos=new ObjectOutputStream(clientSocket.getOutputStream());
		    oos.writeObject(myFile);
		    
		   // FileOutputStream fos = new FileOutputStream("abc.flv");
		   // BufferedOutputStream bos = new BufferedOutputStream(fos);
		   // bytesRead = is.read(mybytearray,0,mybytearray.length);
		   // current = bytesRead;
		    // thanks to A. Cdiz for the bug fix
		    /*do {
		       bytesRead =
		          is.read(mybytearray, current, (mybytearray.length-current));
		       if(bytesRead >= 0) current += bytesRead;
		    } while(bytesRead > -1);
		    bos.write(mybytearray, 0 , current);
		    bos.flush();
		    long end = System.currentTimeMillis();*/
		    System.out.println("Fini");
		}

}

class StuffThread extends Thread 
{
	  private byte[] data = new byte[255];

	  private Socket socket;

	  public StuffThread(Socket socket) {

	    for (int i = 0; i < data.length; i++)
	      data[i] = (byte) i;
	    this.socket = socket;
	  }

	  public void run() {
	    try {
	      OutputStream out = new BufferedOutputStream(socket.getOutputStream());
	      while (!socket.isClosed()) {
	        out.write(data);
	      }
	      socket.close();
	    } catch (Exception e) {
	    }
	  }
}